package com.hex.liyue.giftmodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.IosChoiceDialog;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.giftmodule.adapter.AdapterMyGifts;
import com.hex.liyue.giftmodule.bean.DiscountDetailsBean;
import com.hex.liyue.giftmodule.bean.MyGiftsResultBean;
import com.hex.liyue.giftmodule.presenter.IMyGiftsView;
import com.hex.liyue.giftmodule.presenter.MyGiftsPresenter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的礼物界面
 * Created by WJ on 2016/6/18.
 */
public class MyGiftsActivity extends BaseActivity implements IMyGiftsView, AdapterMyGifts.OnItemClickListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.rv_my_gifts)
    YRecycleview rvMyGifts;
    @Bind(R.id.tv_receipt)
    TextView tvReceipt;
    @Bind(R.id.tv_commutation)
    TextView tvCommutation;
    @Bind(R.id.iv_all)
    ImageView ivAll;
    @Bind(R.id.tv_all)
    TextView tvAll;
    @Bind(R.id.empty_gift)
    LinearLayout emptyGift;

    private MyGiftsPresenter myGiftsPresenter;
    private MyGiftsResultBean myGiftsResultBean;
    private AdapterMyGifts adapterMyGifts;
    private boolean isAll = false;

    @Override
    protected void bindPresenter() {
        myGiftsPresenter = new MyGiftsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        myGiftsPresenter.requestGetMyGiftList();
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_my_gifts;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("我的礼物");
    }


    @OnClick({R.id.iv_toobar_back, R.id.ll_all_box, R.id.tv_receipt, R.id.tv_commutation, R.id.tv_mystore})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.ll_all_box:
                if (isAll) {
                    //全选了  进行全不选
                    for (MyGiftsResultBean.DataBean dataBean : myGiftsResultBean.data) {
                        dataBean.isCheck = false;
                    }
                    adapterMyGifts.notifyDataSetChanged();
                    ivAll.setImageResource(R.drawable.unchecked);
                    tvAll.setText("全选");
                    isAll = false;
                    tvReceipt.setText("收货(0)");
                    tvCommutation.setText("折现(0)");
                } else {
                    //全不选  进行全选
                    for (MyGiftsResultBean.DataBean dataBean : myGiftsResultBean.data) {
                        dataBean.isCheck = true;
                    }
                    adapterMyGifts.notifyDataSetChanged();
                    ivAll.setImageResource(R.drawable.checked);
                    tvAll.setText("全不选");
                    isAll = true;
                    tvReceipt.setText("收货(" + myGiftsResultBean.data.size() + ")");
                    tvCommutation.setText("折现(" + myGiftsResultBean.data.size() + ")");
                }
                break;
            case R.id.tv_receipt:
                //收货
                receipt();
                break;
            case R.id.tv_commutation:
                //折现
                commutation();
                break;
            case R.id.tv_mystore:
                //跳转到我的商城界面
                Intent intent = new Intent(MyGiftsActivity.this, MyGiftsStoresActivity.class);
                intent.putExtra("flag", "flag");
                startActivity(intent);
                break;
        }
    }

    //收货
    private void receipt() {
        int i = 0;//选中几个了
        List<Map<String, Object>> gift_list = new ArrayList<>();
        for (MyGiftsResultBean.DataBean dataBean : myGiftsResultBean.data) {
            if (dataBean.isCheck && dataBean.current_gift_num > 0) {
                i++;
                Map<String, Object> map = new HashMap<>();
                map.put("gift_id", dataBean.gift_id);
                map.put("gift_url", dataBean.gift_url);
                map.put("gift_brand", dataBean.gift_brand);
                map.put("gift_name", dataBean.gift_name);
                map.put("gift_num", dataBean.current_gift_num);
                map.put("gift_price", dataBean.price);
                gift_list.add(map);
            }
        }
        if (i > 0) {
            startConfirmReceiptActivity(gift_list);
        } else {
            showT("请您选择礼物！");
        }
    }

    //折现
    private void commutation() {
        int i = 0;//选中几个了
        double totalPirce = 0;
        final JSONArray gift_list = new JSONArray();
        for (MyGiftsResultBean.DataBean dataBean : myGiftsResultBean.data) {
            if (dataBean.isCheck && dataBean.current_gift_num > 0) {
                i++;
                totalPirce += dataBean.zhexianPrice * dataBean.current_gift_num;

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("gift_id", dataBean.gift_id);
                jsonObject.put("gift_num", dataBean.current_gift_num);
                jsonObject.put("price ", dataBean.zhexianPrice);
                gift_list.add(jsonObject);
            }
        }
        if (i > 0) {
            String user_type = UserManager.getUserType();
            String title = "折现将收到礼物金额的";
            String message = "可折现金额:" + totalPirce + "元";
            if (user_type.equals("1")) {//普通用户  you 50    40
                if (!TextUtils.isEmpty(UserManager.getInviteCode())) {
                    title += "50%";
                } else{
                    title += "40%";
                }
            } else if (user_type.equals("2")) {//公司  60
                title += "60%";

            } else if (user_type.equals("3")) {//大区  64
                title += "64%";
            }

            IosChoiceDialog iosChoiceDialog = new IosChoiceDialog(this, title, message);
            iosChoiceDialog.setOnButtonClickListener(new IosChoiceDialog.OnButtonClickListener() {
                @Override
                public void onOkClickListener() {
                    //确认折现
                    myGiftsPresenter.gift_discount(gift_list);
                }

                @Override
                public void onCancelClickListener() {

                }
            });
            iosChoiceDialog.show();
        } else {
            showT("请您选择礼物！");
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void setAdapter(MyGiftsResultBean myGiftsResultBean) {
        this.myGiftsResultBean = myGiftsResultBean;

        adapterMyGifts = new AdapterMyGifts(myGiftsResultBean);
        adapterMyGifts.setOnItemClickListener(this);
        rvMyGifts.setLayoutManager(new LinearLayoutManager(this));
        rvMyGifts.setReFreshEnabled(false);//禁用刷新;
        rvMyGifts.setLoadMoreEnabled(false);//禁用加载
        rvMyGifts.setAdapter(adapterMyGifts);
        rvMyGifts.setEmptyView(emptyGift);
    }

    @Override
    public void startConfirmReceiptActivity(List<Map<String, Object>> gift_list) {
        Intent intent = new Intent(this, ConfirmReceiptActivity.class);
        intent.putExtra("gift_list", (Serializable) gift_list);
        startActivity(intent);
    }

    @Override
    public void startDiscountDetailsActivity(DiscountDetailsBean discountDetailsBean) {
        //跳转到折现详情
        Intent intent = new Intent(this, DiscountDetailsActivity.class);
        intent.putExtra("user_gift_cash_id", discountDetailsBean.data.user_gift_cash_id);
        startActivity(intent);

        finish();
    }


    @Override
    public void onCheckBoxClickListener() {
        int i = 0;
        for (MyGiftsResultBean.DataBean dataBean : myGiftsResultBean.data) {
            if (dataBean.isCheck) {
                i++;
            }
        }
        if (i == myGiftsResultBean.data.size()) {
            //全选了
            isAll = true;
            ivAll.setImageResource(R.drawable.checked);
            tvAll.setText("全不选");
        } else {
            //没全选
            isAll = false;
            ivAll.setImageResource(R.drawable.unchecked);
            tvAll.setText("全选");
        }
        tvReceipt.setText("收货(" + i + ")");
        tvCommutation.setText("折现(" + i + ")");
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
